﻿using AnQinManagermentBackendAPI.Common;
using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.SaleManagement;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.SaleManagement;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class SaleManagementService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public SaleManagementService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取销售管理列表
         */
        public async Task<RequestResponseModel<PageResult<SaleManagementView>>> GetSaleManagementList(SaleManagementSearchDTO saleManagementSearchDTO)
        {
            //筛选
            var query = _context.SaleManagementView
                .WhereIF(a => a.Code.Contains(saleManagementSearchDTO.Code), !string.IsNullOrEmpty(saleManagementSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(saleManagementSearchDTO.Name), !string.IsNullOrEmpty(saleManagementSearchDTO.Name))
                .WhereIF(a => a.StaffId == saleManagementSearchDTO.StaffId, !string.IsNullOrEmpty(saleManagementSearchDTO.StaffId))
                .WhereIF(a => a.CustomerId == saleManagementSearchDTO.CustomerId, !string.IsNullOrEmpty(saleManagementSearchDTO.CustomerId))
                .WhereIF(a => a.ProjectId == saleManagementSearchDTO.ProjectId, !string.IsNullOrEmpty(saleManagementSearchDTO.ProjectId))
                .WhereIF(a => a.State == saleManagementSearchDTO.State, saleManagementSearchDTO.State != null)
                ;

            //返回列表
            var saleManagement = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(saleManagementSearchDTO.CurrentPage, saleManagementSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<SaleManagementView>(saleManagement, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增销售管理
         */
        public async Task<RequestResponseModel<string>> AddSaleManagement(SaleManagementAddDTO saleManagementAddDTO)
        {
            //查询项目
            var project = await _context.Project.FindAsync(saleManagementAddDTO.ProjectId);

            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "找不到该项目", saleManagementAddDTO.ProjectId);
            }

            //操作数据
            DateTime dateTime = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var saleManagement = new SaleManagementModel
            {
                Id = guid.ToString(),
                Code = saleManagementAddDTO.Code,
                Name = saleManagementAddDTO.Name,
                StaffId = saleManagementAddDTO.StaffId,
                CustomerId = project.CustomerId,
                ProjectId = saleManagementAddDTO.ProjectId,
                State = StateEnum.SaleStateEnum.待确认,
                File = saleManagementAddDTO.File,
                Remark = saleManagementAddDTO.Remark,
                CreateTime = dateTime,
            };

            //新增销售管理
            _context.SaleManagement.Add(saleManagement);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 更新销售管理
         */
        public async Task<RequestResponseModel<string>> PutSaleManagement(SaleManagementUpdateDTO saleManagementUpdateDTO)
        {
            //查找数据
            var query = await _context.SaleManagement.FindAsync(saleManagementUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", saleManagementUpdateDTO.Id);
            };

            //查找项目
            var project = await _context.Project.FindAsync(saleManagementUpdateDTO.ProjectId);

            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "找不到该项目", saleManagementUpdateDTO.ProjectId);
            }

            //生成新数据
            query.Name = saleManagementUpdateDTO.Name;
            query.StaffId = saleManagementUpdateDTO.StaffId;
            query.CustomerId = project.CustomerId;
            query.ProjectId = saleManagementUpdateDTO.ProjectId;
            query.File = saleManagementUpdateDTO.File;
            query.Remark = saleManagementUpdateDTO.Remark;

            //更新部门表
            _context.SaleManagement.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", saleManagementUpdateDTO.Id);
        }

        /**
         * 删除销售管理
         */
        public async Task<RequestResponseModel<string>> DeleteSaleManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.SaleManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.SaleManagement.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 确认销售
         */
        public async Task<RequestResponseModel<string>> ConfirmSaleManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.SaleManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //查找项目表
            var project = await _context.Project.FindAsync(query.ProjectId);

            //如项目不存在则抛出
            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "项目不存在", Id);
            }

            //更改状态
            query.State = StateEnum.SaleStateEnum.已确认;
            project.State = StateEnum.ProjectStateEnum.开发中;

            //更新库
            _context.SaleManagement.Update(query);
            _context.Project.Update(project);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "确认成功", Id);
        }

        /**
         * 取消销售
         */
        public async Task<RequestResponseModel<string>> CancelSaleManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.SaleManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //更改状态
            query.State = StateEnum.SaleStateEnum.已取消;

            //更新库
            _context.SaleManagement.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "取消成功", Id);
        }
    }
}
